from DrissionPage._base.chromium import Chromium
from DrissionPage._configs.chromium_options import ChromiumOptions
from DrissionPage._functions.settings import Settings
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from fake_useragent import UserAgent

from crawler_handler.hefei.hefei_handler import HeFeiHandler
from crawler_handler.jinan.jiana_land_handler import JinanLandHandler
from crawler_handler.jinan.jinan_handler import JinanHandler
from entity.schema.crawler_schema import CrawlerJiNanArgs, CrawlerLandArgs, CrawlerHeFeiArgs
from enums.crawler_enum import ProjectType, CrawlerType


def jinan_task():
    co = ChromiumOptions().headless()
    co.auto_port()
    co.set_retry(2, 30)
    co.set_user_agent(UserAgent().random)
    co.incognito()
    co.ignore_certificate_errors()
    b = Chromium(co)
    JinanHandler(b, CrawlerJiNanArgs(project_type=ProjectType.HOUSE)).crawler_start()
    b.quit()
    # t = b.new_tab()
    # args = CrawlerLandArgs(crawler_type=CrawlerType.LAND)
    # handler = JinanLandHandler(
    #     t,
    #     args
    # )
    # handler.perform_task()

def hefei_task():
    co = ChromiumOptions().headless()
    # co = ChromiumOptions()
    # co.auto_port()
    co.set_local_port(9600)
    # 启用多例：
    co.set_retry(2, 30)
    co.set_load_mode('eager')
    co.set_user_agent(UserAgent().random)
    co.incognito()
    co.ignore_certificate_errors()
    b = Chromium(co)
    # HeFeiHandler(b, CrawlerHeFeiArgs(area='蜀山区')).test()
    HeFeiHandler(b, CrawlerHeFeiArgs()).crawler_project()
    # HeFeiHandler(b, CrawlerHeFeiArgs()).collect_community_project(987)
    # HeFeiHandler(b, CrawlerHeFeiArgs(area='蜀山区')).crawler_room()
    b.quit()

def start_scheduler():
    """初始化调度器并添加任务"""
    scheduler = AsyncIOScheduler()
    # # 每天凌晨9.30点执行一次任务
    # scheduler.add_job(new_house_task, 'cron', hour=9, minute=30, timezone='Asia/Shanghai')
    scheduler.add_job(jinan_task, 'cron', hour=9, minute=26, timezone='Asia/Shanghai')
    # scheduler.add_job(hefei_task, 'cron', hour=11, minute=50, timezone='Asia/Shanghai')
    # 每60秒执行一次任务
    # scheduler.add_job(new_house_task, 'interval', seconds=30, max_instances=2)
    # scheduler.add_job(land_task, 'interval', seconds=30, max_instances=2)
    scheduler.start()
    return scheduler